package com.kangnam.mongo;

import com.kangnam.mongo.MongoManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class InputCustomer
{

	private static MongoManager mongoManager = null;
	private static String DB_NAME;
	private static String C_INFO = "customer";
	
	public InputCustomer(String dbName, String host)
	{
		mongoManager = MongoManager.getInstance();
		mongoManager.createPool(host);
		DB_NAME = dbName;	
	}
	
	public void insertCustomerInfo(String c_LoginID, String c_LoginPass, String c_Name, 
			int c_Age, String c_Sex, String c_Phone, String c_Email, 
			String[] c_Buy_List)
	{
		Mongo mongo = null;
		
		ProduceIdGenerator CIG = new ProduceIdGenerator();
		
		CIG.getInstance();
		
		CIG.getId();
		
		try 
		{
			mongo = mongoManager.getConnection();
			
			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(C_INFO);

			BasicDBObject query = new BasicDBObject();
			query.put("c_ID", CIG.getId());
			query.put("c_LoginID", c_LoginID);
			query.put("c_LoginPass", c_LoginPass);
			query.put("c_Name", c_Name);
			query.put("c_Age", c_Age);
			query.put("c_Sex", c_Sex);
			query.put("c_Phone", c_Phone);
			query.put("c_Email", c_Email);
			query.put("c_Buy_List", c_Buy_List);
			
			
			coll.insert(query);
			
		} 
		
		finally 
		{
			mongoManager.close(mongo);
		}
	}
	
	public static void viewCollection(String collection) {
		Mongo mongo = null;
		
		try{
			mongo = mongoManager.getConnection();
			System.out.println("viewCollection: " + mongo.hashCode());

			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(collection);
			DBCursor cursor = coll.find();
			while (cursor.hasNext()){
				DBObject dbObject = (DBObject) cursor.next();
				System.out.println(dbObject);
			}
		} finally {
			mongoManager.close(mongo);
		}
	}

	
	public static void main(String[] args) 
	{
		InputCustomer InputC = new InputCustomer("db", "127.0.0.1");
		
		String[] c_Buy_List= null;
		
		InputC.insertCustomerInfo("200601281", "1234", "Kevin", 27, "M", 
				"010-8729-9229", "nd0904@nate.com", c_Buy_List);
		
		InputC.viewCollection(C_INFO);
		
	
	}
}
